﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using BuildPlannerLib.Buildings;
using BuildPlannerLib.Enums;
using BuildPlannerLib.PlayerActions;
using BuildPlannerLib.SimEvents;
using BuildPlannerLib.Terran.Buildings;
using BuildPlannerLib.Terran.Enums;

namespace BuildPlannerLib.Terran.PlayerActions
{
    // Derived class from Action for producing an addon on a Barracks/Factory/Starport
    public class ProduceAddon : PlayerAction
    {
        // Field for the building that will produce the addon
        public IndividualAddonBuilding Building;

        // Properties for the type of addon and type of building
        public AddonType addonType;
        public string buildingType;
        private AddonType AddonType
        {
            get { return addonType; }
            set { addonType = value; }
        }
        private string BuildingType
        {
            get { return buildingType; }
            set { buildingType = value; }
        }

        // Constructor with a parameter for the build, building type, and addon type
        public ProduceAddon(Build build, string buildingType, AddonType addonType)
        {
            Build = build;

            BuildingType = buildingType;
            AddonType = addonType;

            MineralCost = 50;
            switch (AddonType)
            {
                case AddonType.Reactor:
                    GasCost = 50;
                    TimeDuration = 5000;
                    break;
                case AddonType.TechLab:
                    GasCost = 25;
                    TimeDuration = 2500;
                    break;
            }
            Description = "Produce " + AddonType + " on " + BuildingType;
            LongDescription = "Producing a new " + AddonType + " on an existing " + BuildingType;
        }

        // Evaluates whether the player is Terran, whether there is mineral income or enough minerals, whether there is gas income or enough gas, and whether an addonless building of the specified type exists
        public override bool RequirementsFilled()
        {
            Error = null;
            
            if (Build.Race != Race.Terran)
            {
                Error = "This building can only be built by Terran players.";
                return false;
            }

            if (AddonType == AddonType.None)
            {
                Error = "Invalid addon type.";
                return false;
            }

            AddonBuilding building = Build.Player.GetBuilding(buildingType) as AddonBuilding;
            if (building == null)
            {
                Error = "Invalid building type.";
                return false;
            }

            bool buildingAvailable = building.IndividualBuildings.Exists(delegate(IndividualBuilding i)
            {
                IndividualAddonBuilding a = i as IndividualAddonBuilding;
                return a.AddonType == AddonType.None;
            });

            if (!buildingAvailable)
            {
                Error = "No addon-less " + BuildingType + " available.";
                return false;
            }

            return base.RequirementsFilled();
        }

        // Evaluates if enough minerals are available, enough gas is available and an addon-less building is not in-use
        public override bool StartCheck()
        {
            AddonBuilding building = Build.Player.GetBuilding(BuildingType) as AddonBuilding;

            bool availableBuilding = building.IndividualBuildings.Exists(delegate(IndividualBuilding i)
            {
                IndividualAddonBuilding a = i as IndividualAddonBuilding;
                return a.AddonType == AddonType.None && !i.InUse;
            });

            if (!availableBuilding)
                return false;

            return base.StartCheck();
        }

        // Spends the minerals, spends the gas, sets the building to the appropiate addon type, and adds the event for finishing the action
        public override void StartAction()
        {
            AddonBuilding building = Build.Player.GetBuilding(BuildingType) as AddonBuilding;

            base.StartAction();

            Building = building.IndividualBuildings.Find(delegate(IndividualBuilding i)
            {
                IndividualAddonBuilding a = i as IndividualAddonBuilding;
                return !i.InUse && a.AddonType == AddonType.None;
            }) as IndividualAddonBuilding;

            switch (AddonType)
            {
                case AddonType.Reactor:
                    Building.AddReactor();
                    break;
                case AddonType.TechLab:
                    Building.AddTechLab();
                    break;
            }
        }

        // Un-sets the InUse and AddonInUse flags after completing production.
        public override void FinishAction()
        {
            base.FinishAction();

            Building.FinishProduction();
            Building.FinishAddonProduction();
        }
    }
}
